<?php 
namespace app\home\controller;
use think\Controller;
use think\Db;	
use think\Session;
use think\request;

//设置密码
class Setpassword extends Base{
	public function index(){
	    $user = $this->userInformation();
	    $this->assign('user',$user);
		return $this->fetch('Setpassword/index');
	}
    public function indexs(){
    	 $user = $this->userInformation();
	    $this->assign('user',$user);
    	return $this->fetch('Setpassword/indexs');
    }
  
    //注册获取短信验证码
    public function registersendMsg(){

        $mobile = $_POST['mobile'];
        if($mobile!=Session::get('phonenumber')) return ['msg'=>'短信发送失败'];
        //一分钟只能发生一次
        $usercode = Db::name('user_code')->where('mobile',$mobile)->order('id desc')->find();
        // dump($usercode);die;
        if($usercode['reg_time']+60 >= time()) return json_encode(['msg'=>'一分钟只能发送一次'],JSON_UNESCAPED_UNICODE);
        $tplParam = rand(1000,9999);
        $tpCode = 'SMS_135026381';//支付密码添加
        $message = sendMsg($mobile,$tpCode,$tplParam);
        // $message = ['Message'=>'OK'];
        if($message['Message']=='OK'){
            $data = [
                'mobile' => $mobile,
                'code'   => $tplParam,
                'reg_time'=> time(),
            ];
            $code = Db::name('user_code')->insert($data);
            if($code){
                return ['msg'=>'success'];
            }else{
                return ['msg'=>'验证码发送错误'];
            }
        }else{
            return ['msg'=>'验证码发送错误'];
        }

    }

    //验证验证码
    public function codeVerification(){
        $param = $this->request->post();
        $mobile = Session::get('phonenumber');
        $code = $param['code'];
        if($mobile == null ){
            return ['msg'=>'手机号不能为空'];
        }
        if($code==null){
            return ['msg'=>'请输入验证码'];
        }else{
            $codes = Db::name('user_code')->where('mobile',$mobile)->order('id desc')->find();
            if($codes['code']==$code){
                if(time()>=$codes['reg_time']+1800){
                    return ['msg'=>'验证码超时'];
                }else{
                    if($codes['state']==1){
                        return ['msg'=>'验证码已经被使用'];
                    }else{
                        return ['msg'=>'success'];
                    }
                }
            }else{
                return ['msg'=>'验证码错误'];
            }
        }

    }

    public function setPwd(){
        $param = myarr($this->request->post());
//        dump($param);die;
        $user = $this->userInformation();
        if($user['password']!=$param['logpwd']) {
            $this->error('登录密码验证错误','Setpassword/index');
        }
        if($param['codes']!=Session::get('codes')){
            $this->success('资料已经提交','Setpassword/index');
        }
        Session::set('codes',mt_rand(1000,9999));
        if(preg_match("/[\',:;*?~`!#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$param['setpwUserPassword'])){
            $this->error('密码格式错误','Setpassword/index');
        }
        if($param['setpwUserPassword']!=$param['setpwUserConfirmPassword']){
            $this->error('两次密码不一致','Setpassword/index');
        }
        $pwd = Md5($user['usercode'].$param['setpwUserPassword']);
        if($pwd==$user['paymentPassword']){
            $this->error('新密码不能和原密码相同','Setpassword/index');
        }

        $up = Db::name('users')->where(['id'=>Session::get('uid')])->update(['paymentPassword'=>$pwd]);
        //添加记录
        if(empty($param['edit'])){
            $ins = Db::name('login_record')
                ->insert([
                    'uid'=>Session::get('uid'),
                    'logintoip'=>$_SERVER['REMOTE_ADDR'],
                    'logintime'=>date('Y-m-d H:i:s'),
                    'operation'=>'添加提现密码']);
        }else{
            $ins = Db::name('login_record')
                ->insert([
                    'uid'=>Session::get('uid'),
                    'logintoip'=>$_SERVER['REMOTE_ADDR'],
                    'logintime'=>date('Y-m-d H:i:s'),
                    'operation'=>'重新设置提现密码']);
        }
        if($up){
            $this->success('支付密码设置成功','Setpassword/index');
        }else{
            $this->error('添加失败','Setpassword/index');
        }
    }

//    public function
}